package com.example;

import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;

import java.io.File;

public class MyServiceServer {
    public static void main(String[] args) throws Exception {
        Server server = ServerBuilder.forPort(50051)
                .useTransportSecurity(new File("path/to/server.crt"), new File("path/to/server.key"))
                .addService(new MyServiceImpl())
                .build()
                .start();

        System.out.println("Server started");
        server.awaitTermination();
    }

    static class MyServiceImpl extends MyServiceGrpc.MyServiceImplBase {
        @Override
        public void myMethod(MyRequest request, StreamObserver<MyResponse> responseObserver) {
            String clientCert = getClientCertificate(); // 获取客户端证书信息
            System.out.println("Client Certificate: " + clientCert);

            MyResponse response = MyResponse.newBuilder().setReply("Hello, " + request.getMessage()).build();
            responseObserver.onNext(response);
            responseObserver.onCompleted();
        }

        private String getClientCertificate() {
            // 获取客户端证书信息的逻辑
            return "Client Certificate Info";
        }
    }
}
